package com.xingfly.web;

import com.xingfly.model.Event;
import com.xingfly.model.Post;
import com.xingfly.model.User;
import com.xingfly.security.jwt.annotation.CurrentUser;
import com.xingfly.service.CommentService;
import com.xingfly.service.EventService;
import com.xingfly.service.PostService;
import com.xingfly.util.PageUtil;
import com.xingfly.util.Type;
import com.xingfly.web.model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * Created by SuperS on 30/12/2016.
 */
@RequestMapping("posts")
@RestController
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private EventService eventService;
    @Autowired
    private CommentService commentService;

    /**
     * 获取所有post
     */
    @GetMapping
    public ResponseModel<?> posts(@RequestParam(value = "page", required = false) String pageStr, @RequestParam(value = "size", required = false) String sizeStr) {
        Integer page = null;
        Integer size = null;
        try {
            page = PageUtil.parsePage(pageStr);
            size = PageUtil.parseSize(sizeStr);
        } catch (NumberFormatException e) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式错误!");
        }
        return ResponseModel.ok(postService.findAll(new PageRequest(page - 1, size)));
    }

    /**
     * 根据ID获取Post
     */
    @GetMapping("{id}")
    public ResponseModel<?> getPostById(@PathVariable("id") Long id) {
        return ResponseModel.ok(postService.findOne(id));
    }

    /**
     * 创建Post
     */
    @PostMapping
    @Transactional
    public ResponseModel<?> myPost(@RequestBody Post post, @CurrentUser User me) {

        post.setAuthor(me);
        Post savePost = postService.create(post);

        Event event = eventService.create(Type.OBJECT_TYPE_POST, savePost);
        return ResponseModel.ok(savePost);
    }

    /**
     * 删除Post
     */
    @DeleteMapping("{id}")
    @Transactional
    public ResponseModel<?> deletePost(@PathVariable("id") Long id, @CurrentUser User user) {
        Post post = postService.findById(id);
        if (post == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "没有该文章!");
        }
        //判断文章的作者是不是当前作者
        if (post.getAuthor().equals(user)) {
            eventService.delete(Type.OBJECT_TYPE_POST, id);
            postService.delete(id);
        } else {
            return ResponseModel.error(HttpStatus.FORBIDDEN, "没有删除权限!");
        }
        return ResponseModel.create(null, HttpStatus.OK, "删除成功!");
    }

    /**
     * 更新Post
     */
    @PutMapping("{id}")
    public ResponseModel<?> putPost(@PathVariable("id") Long id, @CurrentUser User user, @RequestBody Post newPost) {
        Post post = postService.findById(id);
        if (post == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "没有该文章!");
        }
        //判断文章的作者是不是当前作者
        if (post.getAuthor().equals(user)) {
            if (newPost.getContent() != null)
                post.setContent(newPost.getContent());
            if (newPost.getTitle() != null)
                post.setTitle(newPost.getTitle());
            if (newPost.getTags() != null)
                post.setTags(newPost.getTags());
            if (newPost.getExcerpt() != null)
                post.setExcerpt(newPost.getExcerpt());
            postService.create(post);
        } else {
            return ResponseModel.error(HttpStatus.FORBIDDEN, "没有修改权限!");
        }
        return ResponseModel.create(post, HttpStatus.OK, "修改成功!");
    }

    /**
     * 获取文章的所有评论
     */
    @GetMapping("{id}/comments")
    public ResponseModel<?> getPostComments(@PathVariable("id") Long id) {
        if (postService.findById(id) == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "文章不存在");
        }
        return ResponseModel.ok(commentService.getComments(Type.OBJECT_TYPE_POST, id));
    }
}
